{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "86f92339-4dbe-480d-8abf-d969dbb3b64b",
   "metadata": {},
   "source": [
    "Chapter 19\n",
    "# 利用pandas_datareader从FRED下载数据\n",
    "Book_1《编程不难》 | 鸢尾花书：从加减乘除到机器学习  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cb8b0334-cd54-4ba3-8958-617258a67a39",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 导入包\n",
    "import pandas_datareader as pdr\n",
    "# 需要安装 pip install pandas-datareader\n",
    "import pandas as pd\n",
    "import datetime"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "970555c4-65ce-4e3a-b5b3-319392a1a0ff",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 从FRED下载标普500 (S&P 500)\n",
    "start_date = datetime.datetime(2014, 1, 1)\n",
    "end_date   = datetime.datetime(2022, 12, 31)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f44ead76-7599-45e0-a3ed-e0282917e40d",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 下载数据\n",
    "ticker_list = ['SP500']\n",
    "df = pdr.DataReader(ticker_list, 'fred', \n",
    "                    start_date, end_date)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d273e83e-672b-474e-b334-74651df065df",
   "metadata": {},
   "outputs": [],
   "source": [
    "df.head()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
